﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateAndLambda
{
    public delegate void QuKuaiTi();//1.定义委托类型  该委托方法是无参无返回类型
    public delegate int WeiTuo(int a,int b);//1.定义委托类型  该委托方法是有两个参数有返回类型

    internal class Program
    {
        static void Main(string[] args)
        {
            //面向对象：封装、继承和多态
            //对象实例是通过new 类名();来创建
            Person person = new Person();
            person.age = 20;
            person.name = "小明";
            person.eat();

            //person.StuID = "20230101";
            //person.Study();

            Student student = new Student();
            student.age = 19;
            student.name = "小红";
            student.eat();

            student.StuID = "20230101";
            student.Study();

            //3.委托
            //student.fun1 = new QuKuaiTi(fun1);
            //student.fun1 += new QuKuaiTi(fun2);
            //匿名函数委托
            //student.fun1 = delegate ()
            //{
            //    Console.WriteLine("帮我取邮政快递！");
            //};
            //student.fun1 += delegate ()
            //{
            //    Console.WriteLine("帮我取顺丰快递！");
            //};

            //Lambda表达式   是匿名函数的更加精简版
            student.fun1 = () =>
            {
                Console.WriteLine("帮我取邮政快递！");
            };
            student.fun1 += () =>
            {
                Console.WriteLine("帮我取顺丰快递！");
            };


            //student.fun2 += new WeiTuo(fun3);
            //student.fun2 += new WeiTuo(fun4);

            //student.fun2 += delegate (int x, int y)
            //{
            //    Console.WriteLine($"{x} + {y} = {x + y}");
            //    return x + y;
            //};
            //student.fun2 += delegate (int x, int y)
            //{
            //    Console.WriteLine($"{x} * {y} = {x * y}");
            //    return x * y;
            //};

            //Lambda表达式
            student.fun2 += (int x, int y) =>
            {
                Console.WriteLine($"{x} + {y} = {x + y}");
                return x + y;
            };
            student.fun2 += (int x, int y) =>
            {
                Console.WriteLine($"{x} * {y} = {x * y}");
                return x * y;
            };

            //委托学生帮忙去做两位数的求和运算和求积运算(有两个参数，有返回值的方法)


            //4.执行
            student.fun1();
            student.fun2(2,3);

            Console.ReadKey();
        }

        public static void fun1()
        {
            Console.WriteLine("帮我取邮政快递！");
        }

        public static void fun2()
        {
            Console.WriteLine("帮我取顺丰快递！");
        }

        public static int fun3(int x,int y)
        {
            Console.WriteLine($"{x} + {y} = {x + y}");
            return x + y;
        }

        public static int fun4(int x, int y)
        {
            Console.WriteLine($"{x} * {y} = {x * y}");
            return x * y;
        }
    }
}
